Page({
    data: {
        courses: [
            {
                id: 999,
                title: '视频播放测试课程',
                teacher: '霍丙乾bennyhuo',
                cover: '../../images/avatar-default.svg',
                duration: '55分钟',
                progress: 0,
                totalVideos: 3,
                watchedVideos: 0,
                description: '这是一个测试课程，包含三个视频，用于演示视频播放功能',
                status: 'learning',
                category: '测试'
            },
            {
                id: 1,
                title: '数据结构与算法',
                teacher: '李教授',
                cover: '/images/course1.jpg',
                duration: '40小时',
                progress: 75,
                totalVideos: 20,
                watchedVideos: 15,
                description: '计算机科学基础课程，学习数据结构和算法设计',
                status: 'learning',
                category: '计算机科学'
            },
            {
                id: 2,
                title: 'Web前端开发',
                teacher: '王老师',
                cover: '/images/course2.jpg',
                duration: '35小时',
                progress: 30,
                totalVideos: 18,
                watchedVideos: 5,
                description: 'HTML、CSS、JavaScript基础及实战项目',
                status: 'learning',
                category: '前端开发'
            },
            {
                id: 3,
                title: '数据库原理',
                teacher: '张教授',
                cover: '/images/course3.jpg',
                duration: '30小时',
                progress: 100,
                totalVideos: 15,
                watchedVideos: 15,
                description: '关系型数据库设计与SQL语言应用',
                status: 'completed',
                category: '数据库'
            },
            {
                id: 4,
                title: '计算机网络',
                teacher: '赵老师',
                cover: '/images/course4.jpg',
                duration: '45小时',
                progress: 0,
                totalVideos: 25,
                watchedVideos: 0,
                description: '网络协议、网络安全、网络编程基础',
                status: 'not_started',
                category: '计算机网络'
            },
            {
                id: 5,
                title: 'Python编程基础',
                teacher: '刘老师',
                cover: '/images/course5.jpg',
                duration: '38小时',
                progress: 60,
                totalVideos: 22,
                watchedVideos: 13,
                description: 'Python语言基础语法、数据结构、面向对象编程',
                status: 'learning',
                category: '编程语言'
            },
            {
                id: 6,
                title: '移动应用开发',
                teacher: '陈教授',
                cover: '/images/course6.jpg',
                duration: '42小时',
                progress: 0,
                totalVideos: 28,
                watchedVideos: 0,
                description: '微信小程序开发、React Native跨平台开发',
                status: 'not_started',
                category: '移动开发'
            }
        ],
        filteredCourses: [],
        searchKeyword: '',
        filterType: 'all', // all, learning, completed, not_started
        showSearch: false
    },

    onLoad() {
        this.loadCourses();
        this.updateFilteredCourses();
    },

    // 加载课程数据
    loadCourses() {
        // 这里应该从服务器获取课程数据
        // 暂时使用模拟数据
        const storedCourses = wx.getStorageSync('studentCourses');
        if (storedCourses && storedCourses.length > 0) {
            this.setData({
                courses: storedCourses
            });
        } else {
            // 如果本地没有数据，使用默认数据并保存到本地存储
            wx.setStorageSync('studentCourses', this.data.courses);
        }
        this.updateFilteredCourses();
    },

    // 切换搜索框显示状态
    toggleSearch() {
        this.setData({
            showSearch: !this.data.showSearch
        });
        if (!this.data.showSearch) {
            this.setData({
                searchKeyword: ''
            });
            this.updateFilteredCourses();
        }
    },

    // 搜索输入处理
    onSearchInput(e) {
        const keyword = e.detail.value.trim();
        this.setData({
            searchKeyword: keyword
        });
        this.updateFilteredCourses();
    },

    // 筛选课程
    filterCourses(e) {
        const filterType = e.currentTarget.dataset.type;
        this.setData({
            filterType
        });
        this.updateFilteredCourses();
    },

    // 更新筛选后的课程列表
    updateFilteredCourses() {
        const { courses, filterType, searchKeyword } = this.data;
        let filtered = courses;

        // 状态筛选
        if (filterType !== 'all') {
            filtered = filtered.filter(course => course.status === filterType);
        }

        // 关键词搜索
        if (searchKeyword) {
            filtered = filtered.filter(course => 
                course.title.toLowerCase().includes(searchKeyword.toLowerCase()) ||
                course.teacher.toLowerCase().includes(searchKeyword.toLowerCase()) ||
                course.description.toLowerCase().includes(searchKeyword.toLowerCase()) ||
                course.category.toLowerCase().includes(searchKeyword.toLowerCase())
            );
        }

        this.setData({
            filteredCourses: filtered
        });
    },

    // 清空搜索
    clearSearch() {
        this.setData({
            searchKeyword: '',
            showSearch: false
        });
        this.updateFilteredCourses();
    },

    // 进入课程详情/开始学习
    enterCourse(e) {
        const courseId = e.currentTarget.dataset.id;
        const course = this.data.courses.find(c => c.id === courseId);

        if (course.status === 'not_started') {
            wx.showModal({
                title: '提示',
                content: '该课程尚未开始，请等待开课通知',
                showCancel: false
            });
            return;
        }

        wx.navigateTo({
            url: `/pages/student/video-player/video-player?courseId=${courseId}`
        });
    },

    // 查看学习进度
    viewProgress(e) {
        e.stopPropagation();
        const courseId = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: `/pages/student/learning-progress/learning-progress?courseId=${courseId}`
        });
    },

    // 下拉刷新
    onPullDownRefresh() {
        this.loadCourses();
        wx.stopPullDownRefresh();
    }
}); 